//
//  CircleLoadingView.m
//  HYCircleLoadingView
//
//  Created by mac on 15/1/19.
//  Copyright (c) 2015年 mac. All rights reserved.
//

#import "CircleLoadingView.h"
#define ANGEL(a) 2*M_PI/360*(a)
#define kAnimationKeyPath   @"transform.rotation.z"

@interface CircleLoadingView()

@property (nonatomic) CGFloat angelPer;
@property (nonatomic,strong) NSTimer *timer;

@end

@implementation CircleLoadingView


- (id)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor clearColor];
    }
    return self;
}

- (id)init{
    self = [super init];
    if (self) {
        self.backgroundColor = [UIColor clearColor];
    }
    return self;
}

- (void)setAngelPer:(CGFloat)angelPer{
    _angelPer = angelPer;
    [self setNeedsDisplay];
}

- (void)startAnimation{
    if (self.isAnimation) {
        [self stopAnimation];
        [self.layer removeAllAnimations];
    }
    
    _isAnimation = YES;
    self.angelPer = 0;
    self.timer = [NSTimer scheduledTimerWithTimeInterval:0.02f
                                                  target:self
                                                selector:@selector(drawPathAnimation:)
                                                userInfo:nil
                                                 repeats:YES];
}

- (void)drawPathAnimation:(NSTimer *)timer{
    self.angelPer += 0.03f;
    if (self.angelPer>=1) {
        self.angelPer = 1;
        [self.timer invalidate];
        self.timer = nil;
        [self startRotationAnimation];
    }
}



- (void)startRotationAnimation{
    CABasicAnimation *basicAnimation = [CABasicAnimation animationWithKeyPath:[NSString stringWithFormat:@"%@",kAnimationKeyPath]];
    basicAnimation.fromValue = @(0);
    basicAnimation.toValue = @(2*M_PI);
    basicAnimation.duration = 1.0f;
    basicAnimation.repeatCount = INT_MAX;
    [self.layer addAnimation:basicAnimation forKey:@"keyFrameAnimation"];
}

- (void)stopAnimation{
    _isAnimation = NO;
    if ([self.timer isValid]) {
        [self.timer invalidate];
        self.timer = nil;
    }
    [self stopRotateAnimation];
}

- (void)stopRotateAnimation{
    [UIView animateWithDuration:0.3f
            animations:^{
                self.alpha = 0;
    } completion:^(BOOL finished) {
        self.angelPer = 0;
        [self.layer removeAllAnimations];
        self.alpha = 1.0f;
    }];
}


- (void)drawRect:(CGRect)rect{
    if (self.angelPer<=0) {
        _angelPer = 0;
    }
    CGFloat lineWidth = 1.f;
    UIColor *lineColor = [UIColor lightGrayColor];
    if (self.lineWidth) {
        lineWidth = self.lineWidth;
    }
    
    if (self.lineColor) {
        lineColor = self.lineColor;
    }
    
    CGContextRef ref = UIGraphicsGetCurrentContext();
    CGContextSetLineWidth(ref, lineWidth);
    CGContextSetStrokeColorWithColor(ref, lineColor.CGColor);
    CGContextAddArc(ref, CGRectGetMidX(self.bounds), CGRectGetMidY(self.bounds), CGRectGetWidth(self.bounds)/2-lineWidth, ANGEL(120), ANGEL(120)+ANGEL(330)*_angelPer, 0);
    CGContextStrokePath(ref);
    
    
}


@end
